package credits;

import game.Constants;
import game.TwistyBasicGameState;

import java.util.ArrayList;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import controllers.PlayerController;
import paintball.PaintballPlayer;
import vision.Vision;

public class Credits extends TwistyBasicGameState{
	public Credits(Vision vision) {
		super(vision);
	}

	Image credits;
	
	int imgX = 150, imgY = 50;
	float imgRotation = 0;
	ArrayList<Float> rotationList = new ArrayList<Float>();
	
	@Override
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {
		credits = new Image("res/credits.png");
		try{
			rotationList = this.getNewRotationArrayList(PlayerController.getPlayers());
		}catch(Exception e){
			
		}
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		
		g.rotate(imgX + (credits.getWidth()/2), imgY + (credits.getHeight()/2), imgRotation);
		g.drawImage(credits, imgX, imgY);
		g.resetTransform();

	}
	
	public ArrayList<Float> getNewRotationArrayList(ArrayList<PaintballPlayer> playerList){
		ArrayList<Float> result = new ArrayList<Float>();

		for(PaintballPlayer p : playerList){
			result.add((float)p.getRotation()+90);
		}

		return result;
	}
	
	@Override
	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		super.update(container, game, delta);

		try{
			for(int i = 0; i < rotationList.size(); i++){
				if(rotationList.get(i) != getNewRotationArrayList(PlayerController.getPlayers()).get(i)){
					this.imgRotation = getNewRotationArrayList(PlayerController.getPlayers()).get(i);
					rotationList = getNewRotationArrayList(PlayerController.getPlayers());
					break;
				}
			}
			rotationList = this.getNewRotationArrayList(PlayerController.getPlayers());
		}catch(Exception e){
			
		}
	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return Constants.credits;
	}
}
